require 'components/toolbar'

class Document < Pan::Component

  def initialize(body, title="N/A")
    @title = title
    @body = body
    @css = []
    @scripts = []
    @meta = []
    @toolbar = Pan::Toolbar.new
  end
  
  def add_css(url)
    @css << url
  end

  def add_script(url)
    @scripts << url
  end

  def add_meta(meta)
    @meta << meta
  end

  def render_on(html)
    html.doctype_transitional
    html.html("xmlns"=>"http://www.w3.org/1999/xhtml", "lang"=>"en") {
      html.head() {
        html.title(@title)
        @meta.each do |meta|
          html.text(meta)
        end
        @css.each do |url|
          html.text("<link type=\"text/css\" href=\"#{url}\" rel=\"stylesheet\"/>")
        end
        @scripts.each do |url|
          html.text("<script type=\"text/javascript\" src=\"#{url}\"></script>")
        end
      }
      html.body {
        if block_given?
          yield(html)
        else
          html.render(@body)
          html.render(@toolbar) unless $DEPLOY
        end
      }
    }
  end

  def callbacks_done(chain)
    @body.callbacks_done(chain << self)
  end

end
